//
// Created by LONG on 2020/11/2.
//

#ifndef O5G_SRC_CCORE_MISC_INCLUDE_TOMLMACRO_H_
#define O5G_SRC_CCORE_MISC_INCLUDE_TOMLMACRO_H_

#include "../../include/commDef.h"
#include "../../../../lib/3rdParty/toml/toml.h"

#ifdef __cplusplus
extern "C"
{
#endif

#define TOML_GET_INT_VALUE(TABLE, KEY, INT_VAL) \
do {\
    int32_t macroRc;\
    int64_t macroValue;\
    toml_raw_t tomlRawData;\
    tomlRawData = toml_raw_in(TABLE, KEY);\
    if (NULL == tomlRawData)\
    {\
        CSP_LOG_ERR(0, "[%s, %d] toml_raw_in() failed!\n", (char *)__func__, __LINE__);\
        return O5G_RC_ER;\
    }\
    rc = toml_rtoi(tomlRawData, &macroValue);\
    if (0 != rc)\
    {\
        CSP_LOG_ERR(0, "[%s, %d] toml_rtoi() failed!\n", (char *)__func__, __LINE__);\
        return O5G_RC_ER;\
    }\
    INT_VAL = (typeof(INT_VAL))macroValue;\
} while (0)

#define TOML_GET_BOOL_VALUE(TABLE, KEY, BOOL_VAL) \
do {\
    int32_t macroRc;\
    int macroValue;\
    toml_raw_t tomlRawData;\
    tomlRawData = toml_raw_in(TABLE, KEY);\
    if (NULL == tomlRawData)\
    {\
        CSP_LOG_ERR(0, "[%s, %d] toml_raw_in() failed!\n", (char *)__func__, __LINE__);\
        return O5G_RC_ER;\
    }\
    rc = toml_rtob(tomlRawData, &macroValue);\
    if (0 != rc)\
    {\
        CSP_LOG_ERR(0, "[%s, %d] toml_rtob() failed!\n", (char *)__func__, __LINE__);\
        return O5G_RC_ER;\
    }\
    BOOL_VAL = macroValue ? TRUE : FALSE;\
} while (0)

#define TOML_GET_STR_VALUE(TABLE, KEY, STR_VAL) \
do {\
    int32_t macroRc;\
    toml_raw_t tomlRawData;\
    tomlRawData = toml_raw_in(TABLE, KEY);\
    if (NULL == tomlRawData)\
    {\
        CSP_LOG_ERR(0, "[%s, %d] toml_raw_in() failed!\n", (char *)__func__, __LINE__);\
        return O5G_RC_ER;\
    }\
    rc = toml_rtos(tomlRawData, &STR_VAL);\
    if (0 != rc)\
    {\
        CSP_LOG_ERR(0, "[%s, %d] toml_rtos() failed!\n", (char *)__func__, __LINE__);\
        return O5G_RC_ER;\
    }\
} while (0)

#ifdef __cplusplus
}
#endif

#endif //O5G_SRC_CCORE_MISC_INCLUDE_TOMLMACRO_H_
